#coding=utf-8

from DjangoBbs.Common.Modifier import ClassManagerClassId, ClassManager
from PublicTool import HttpWrite
from PublicTool.ViewHelper import ViewHelper
import models,forms
__author__ = 'czq'
class ThemeTypeView(ViewHelper):
    def __init__(self):
        self.moduleName='BbsSetting'
        self.typeName='themeType'
    def get_model(self):
        return models.BbsThemeType
    def list_get_dict(self,request):
        return {'bbsClass':self.bbsClass}
    def list_get_list(self,request):
        return models.BbsThemeType.objects.filter(classId=self.bbsClass)
    @ClassManager
    def list(self,request,classId=None):
        if classId!=None:
            from DjangoBbs.BbsTheme.models import BbsClass
            self.bbsClass=BbsClass.objects.get(id=classId)
        return super(ThemeTypeView,self).list(request)

    def add_get_dict(self,request):
        classId=request.GET.get('classId','')
        return {'classId':classId}
        #raise Exception,request
        #return {}
        #return {'url':request.META['HTTP_REFERRER']}
    def add_save_prev(self,request,instance,change):
        if not change:
            classId=request.GET.get('classId','')
            from DjangoBbs.BbsTheme.models import BbsClass
            bbsClass=BbsClass.objects.get(id=classId)#get_object_or_404(BbsClass,pk=int(classId))
            instance.classId=bbsClass
            instance.addUser=request.user
        return True
    def add_response_succeed(self,request,dict,instance):
        return super(ThemeTypeView,self).showUrlReverse('themeType_list',args=(instance.classId.id,))
    def add_get_form(self,request):
        return forms.BbsThemeTypeForm
    @ClassManagerClassId
    def add(self,request,id='0'):
        return super(ThemeTypeView,self).add(request,id)

    def delete_response(self,request,instance):
        return super(ThemeTypeView,self).showUrlReverse('themeType_list',args=(instance.classId.id,))
    def delete_prev(self,request,instance):
        from DjangoBbs.BbsTheme.models import BbsTheme
        #raise Exception,
        if BbsTheme.objects_show.filter(themeType=instance).count()>0:
            return HttpWrite.infoFailure(request,u'不能删除有主题的分类！')
        return True
    @ClassManagerClassId
    def delete(self,request,id=''):
        return super(ThemeTypeView,self).delete(request,id)